<?php
class Controller_App_Login extends Controller_App_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:app/login";
        parent::__construct($req, $res);
    }

    public function action_index ($login_status = null)
    {
        if (! empty($this->_post))
        {
            try {
	            $mobile   = Arr::get($this->_post, 'mobile');
	            $password = Arr::get($this->_post, 'password');
                $referrer = Arr::get($this->_post, 'referrer', '/');
	
	            $uid = $this->_user->login($mobile, $password);
                if (FALSE === $uid)
                    throw new Exception("登陆错误");
                else
	                $this->request->redirect($referrer);
            }
            catch (Exception $e)
            {
                //
                $this->request->redirect('/login/index/1');
            }
        }
        else
        {
            $referrer = $this->request->referrer();
            
            if (site::wechat())
            {
                Cookie::set("referrer_weixin", $referrer, 0);
            }
            
            if (site::wechat()){
                $this->template->set('site', 'weixin');
            } else {
                $this->template->set('site', 'other');
            }

            $this->template->set('referrer', $referrer);
            if($login_status == 1){
                $this->template->set('login_status', 1);
            }else{
                $this->template->set('login_status', 0);
            }
        }
        
        $title_json  = App_Util::buildClientTitleJson('back', 'none', '登陆', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }
    
    public function action_verify_weixin()
    {
        $uid = Passport::getCurrentWeixinUserUid();
        
        $referrer = Cookie::get("referrer_weixin");

        if(empty($referrer) 
            || preg_match('/\/user\/logout/', $referrer) 
            || preg_match('/\/login/', $referrer) )
        {
            $this->request->redirect('/');
        } 
        else
        {
            $this->request->redirect($referrer);
        }
    }

    public function action_forget($forget_status = null)
    {
        if (! empty($this->_post))
        {
            try {
	            $mobile = Arr::get($this->_post, 'mobile');
	            $code   = Arr::get($this->_post, 'code');
	            $password = Arr::get($this->_post, 'password');

	            $uid = $this->_user->forget($mobile, $code, $password);
                    if($uid){
                        $this->request->redirect('/user/ucenter');
                    }else{
                        throw new Exception("找回密码错误");
                    }
	            
            }
            catch (Exception $e)
            {
                //
                $this->request->redirect('/login/forget/1');
            }
                
        }
        else
        {
            if($forget_status == 1){
                $this->template->set('forget_status', 1);
            }else{
                $this->template->set('forget_status', 0);
            }
        }
        
        $this->template->set_filename('smarty:app/login_forget');
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '找回密码', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    public function before ()
    {
        parent::before();
        if ($this->_uid > 0)
        {
            $this->request->redirect('/');
            exit;
        }
    }
}
